<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Sidebar\Tag;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Plugin\AutoTag\Action\Dashboard\Traits\AutoContactTag;

/**
 * 设置客户企业标签.
 *
 * Class CreateContactTag
 * @Controller
 */
class CreateContactTag extends AbstractAction
{
    use AutoContactTag;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @var
     */
    protected $contactTag;

    /**
     * @Inject
     * @var WorkContactContract
     */
    private $workContactContract;

    /**
     * @Middlewares({
     *     @Middleware(SidebarAuthMiddleware::class)
     * })
     * @RequestMapping(path="/sidebar/workContact/createContactTag", methods="POST")
     */
    public function handle(): array
    {
        $params['tagIds'] = $this->request->input('tagIds');
        $params['contactId'] = (int) $this->request->input('contactId', 0);

        $data['tagArr'] = $params['tagIds'];
        $data['contactId'] = $params['contactId'];
        // 员工id
        $data['employeeId'] = user()['id'];
        // 客户
        $contact = $this->workContactContract->getWorkContactById($params['contactId'], ['wx_external_userid']);
        $data['contactWxExternalUserid'] = $contact['wxExternalUserid'];
        // 员工
        $employee = $this->workEmployeeContract->getWorkEmployeeById(user()['id'], ['wx_user_id']);
        $data['employeeWxUserId'] = $employee['wxUserId'];
        $this->autoTag($data);
        $this->deleteTag($data);

        return [];
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'tagIds' => 'required|array',
            'contactId' => 'required|integer|min:1|bail',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'tagIds.required' => '标签id 必传',
            'contactId.required' => '客户id必传',
        ];
    }
}
